const express = require("express")
const router = express.Router()
const { PropertyStaff, OutsourceStaff } = require("../models")

// 通用属性列表
const commonAttributes = [
  "id",
  "name",
  "gender",
  "employee_id",
  "position",
  "phone",
  "join_date",
  "created_at",
  "updated_at",
]

// 获取物业人员列表
router.get("/getPropertyStaff", async (req, res) => {
  try {
    const staff = await PropertyStaff.findAll({
      where: { deleted_at: null },
      order: [["created_at", "DESC"]],
      attributes: [...commonAttributes, "department"],
    })
    res.json({ code: 200, data: staff, message: "获取物业人员列表成功" })
  } catch (error) {
    res.json({ code: 500, message: "获取物业人员列表失败" })
  }
})

// 添加物业人员
router.post("/addPropertyStaff", async (req, res) => {
  try {
    const staff = await PropertyStaff.create(req.body)
    res.json({ code: 200, data: staff, message: "添加物业人员成功" })
  } catch (error) {
    res.json({ code: 500, message: "添加物业人员失败" })
  }
})

// 更新物业人员
router.put("/updatePropertyStaff/:id", async (req, res) => {
  try {
    const staff = await PropertyStaff.findOne({
      where: { id: req.params.id, deleted_at: null },
      attributes: [...commonAttributes, "department"],
    })
    if (!staff) return res.json({ code: 404, message: "未找到该人员" })
    await staff.update(req.body)
    res.json({ code: 200, message: "更新物业人员成功" })
  } catch (error) {
    res.json({ code: 500, message: "更新物业人员失败" })
  }
})

// 删除物业人员
router.delete("/deletePropertyStaff/:id", async (req, res) => {
  try {
    const staff = await PropertyStaff.findByPk(req.params.id)
    if (!staff) return res.json({ code: 404, message: "未找到该人员" })
    await staff.destroy({ force: true })
    res.json({ code: 200, message: "删除物业人员成功" })
  } catch (error) {
    res.json({ code: 500, message: "删除物业人员失败" })
  }
})

// 获取外包人员列表
router.get("/getOutsourceStaff", async (req, res) => {
  try {
    const staff = await OutsourceStaff.findAll({
      where: { deleted_at: null },
      order: [["created_at", "DESC"]],
      attributes: [...commonAttributes, "company"],
    })
    res.json({ code: 200, data: staff, message: "获取外包人员列表成功" })
  } catch (error) {
    res.json({ code: 500, message: "获取外包人员列表失败" })
  }
})

// 添加外包人员
router.post("/addOutsourceStaff", async (req, res) => {
  try {
    const staff = await OutsourceStaff.create(req.body)
    res.json({ code: 200, data: staff, message: "添加外包人员成功" })
  } catch (error) {
    res.json({ code: 500, message: "添加外包人员失败" })
  }
})

// 更新外包人员
router.put("/updateOutsourceStaff/:id", async (req, res) => {
  try {
    const staff = await OutsourceStaff.findOne({
      where: { id: req.params.id, deleted_at: null },
      attributes: [...commonAttributes, "company"],
    })
    if (!staff) return res.json({ code: 404, message: "未找到该人员" })
    await staff.update(req.body)
    res.json({ code: 200, message: "更新外包人员成功" })
  } catch (error) {
    res.json({ code: 500, message: "更新外包人员失败" })
  }
})

// 删除外包人员
router.delete("/deleteOutsourceStaff/:id", async (req, res) => {
  try {
    const staff = await OutsourceStaff.findByPk(req.params.id)
    if (!staff) return res.json({ code: 404, message: "未找到该人员" })
    await staff.destroy({ force: true })
    res.json({ code: 200, message: "删除外包人员成功" })
  } catch (error) {
    res.json({ code: 500, message: "删除外包人员失败" })
  }
})

module.exports = router
